package com.apes.framework.plugin.condition.model;

import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * 功能：条件表通用字段明细
 *
 * @author xul
 * @create 2018-11-06 17:10
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ConditionTableGeneral.CONST_TABLE_NAME, indexes = {@Index(columnList = "condition_table_id, field_Catalog_Item_id", unique = true)})
public class ConditionTableGeneral implements ApesBean {
    public static final String CONST_TABLE_NAME = "tech_condition_table_general";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "条件表通用字段明细")
    @Comment("编号")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "condition_table_id", referencedColumnName = "id", nullable = false)
    @Comment("条件表")
    private ConditionTable conditionTable;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "field_Catalog_Item_id", referencedColumnName = "id", nullable = false)
    @Comment("条件字段目录明细")
    private FieldCatalogItem fieldCatalogItem;

    @Column(name = "main", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("是否主表字段（单据表）")
    private boolean main = false;

    @Column(name = "exec", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("是否执行表")
    private boolean exec = false;

    @Column(name = "exec_main", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("是否执行表主表")
    private boolean execMain = false;


    @Transient
    private ConditionField conditionField;

    public ConditionField getConditionField(){
        if(this.fieldCatalogItem != null){
            return this.fieldCatalogItem.getConditionField();
        }
        return conditionField;
    }

}
